package com.hexinfo.dmpro.monitor.controller;


import cn.hutool.core.bean.BeanUtil;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.common.annotation.Log;
import com.hexinfo.dmpro.common.base.BaseMapper;
import com.hexinfo.dmpro.common.base.BaseService;
import com.hexinfo.dmpro.common.base.Page;
import com.hexinfo.dmpro.monitor.model.CmStatementAudit.*;
import com.hexinfo.dmpro.monitor.service.CmStatementAuditService;
import org.springframework.boot.autoconfigure.jersey.JerseyProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/cmStatementAudit")
public class CmStatementAuditController {

    @Resource
    private CmStatementAuditService statementAuditService;

    @Log("查询拼接的集群名称")
    @GetMapping("/selectClusterName")
    public ResponseEntity<Object> selectClusterName() {
        List<Map<String, String>> list = statementAuditService.selectClusterName();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @Log("组件审计汇总")
    @PostMapping("/selectAuditAlarm")
    public ResponseEntity<Object> selectAuditAlarm(ComponentMonthModel model) {
        Map<String, Object> map = statementAuditService.selectAuditAlarm(model);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @Log("组件详情")
    @GetMapping("/selectComponentDetails")
    public ResponseEntity<Object> selectComponentDetails(ComponentMonthModel model, Page page) {
        Map<String, Object> map = statementAuditService.selectComponentDetails(model,page);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /**
     * 模型审计汇总-模型名称下拉
     * @param component
     * @return
     */
    @Log("根据类型查询模型名称")
    @PostMapping("/selectModelNameByCluster")
    public ResponseEntity<Object> selectModelNameByCluster(String component) {
        List<String> list = statementAuditService.selectModelNameByCluster(component);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @Log("模型审计汇总查询")
    @PostMapping("/selectModelSummary")
    public ResponseEntity<Object> selectModelSummary(ComponentMonthModel model) {
        Map<String, Object> map = statementAuditService.selectModelSummary(model);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @Log("模型详情")
    @GetMapping("/selectModelDetails")
    public ResponseEntity<Object> selectModelDetails(ComponentMonthModel model, Page page) {
        Map<String, Object> map = statementAuditService.selectModelDetails(model, page);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @Log("查询模型配置")
    @GetMapping("/findConfigModel")
    public ResponseEntity<Object> findConfigModel(ConfigureModel model, Page page) {
        Map<String, Object> map = statementAuditService.findConfigModel(model, page);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @Log("数据插入模型配置")
    @PostMapping("/insertConfigModel")
    public ResponseEntity<Object> insertConfigModel(@RequestBody ConfigureModel model) {
        statementAuditService.insertConfigModel(model);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("修改模型配置")
    @PostMapping("/updateConfigModel")
    public ResponseEntity<Object> updateConfigModel(@RequestBody ConfigureModel model) {
        statementAuditService.updateConfigModel(model);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除模型配置")
    @PostMapping("/deleteConfigModel")
    public ResponseEntity<Object> deleteConfigModel(String id) {
        statementAuditService.deleteConfigModel(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("查询大数据语句参数")
    @PostMapping("/selectParam")
    public ResponseEntity<Object> selectParamMonth(@RequestBody Map<Object,Object> map) {
        Page page = new Page();
        page.setPageIndex((Integer) map.get("pageIndex"));
        map.remove("pageIndex");
        page.setPageSize((Integer) map.get("pageSize"));
        map.remove("pageSize");
        ComponentMonthModel model = BeanUtil.mapToBean(map,ComponentMonthModel.class,true);

        if ("IMPALA".equals(model.getComponent())) {
//            List<ImpalaParameterModel> list = statementAuditService.selectImpalaParamMonth(model);
            return new ResponseEntity<>(statementAuditService.selectImpalaParamMonth(model,page), HttpStatus.OK);
        }
        if ("YARN".equals(model.getComponent())) {
//            List<YarnParameterModel> list = statementAuditService.selectYarnParamMonth(model);
            return new ResponseEntity<>(statementAuditService.selectYarnParamMonth(model,page), HttpStatus.OK);
        }
        return new ResponseEntity<>(new ArrayList(), HttpStatus.OK);
    }

    @Log("下载excel")
    @PostMapping("/exportExcel")
    public ResponseEntity<Object> exportExcel(@RequestBody ComponentMonthModel model) {
        List list = new ArrayList();
        if ("IMPALA".equals(model.getComponent())) {
            list = statementAuditService.exportImpalaExcel(model);
            return new ResponseEntity<>(list, HttpStatus.OK);
        }
        if ("YARN".equals(model.getComponent())) {
            list = statementAuditService.exportYarnExcel(model);
            return new ResponseEntity<>(list, HttpStatus.OK);
        }
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @Log("查询impala任务同类语句并发数")
    @PostMapping("/selectImpalaConcurrency")
    public ResponseEntity<Object> selectImpalaConcurrency(String queryType, String tableInfo, String createTime) throws Exception {
        int i = statementAuditService.selectImpalaConcurrency(queryType, tableInfo, createTime);
        return new ResponseEntity<>(i, HttpStatus.OK);
    }

    @Log("查询mapreduce任务同类语句并发数")
    @PostMapping("/selectMapreduceConcurrency")
    public ResponseEntity<Object> selectMapreduceConcurrency(String tableInfo, String createTime) throws Exception {
        int i = statementAuditService.selectMapreduceConcurrency(tableInfo, createTime);
        return new ResponseEntity<>(i, HttpStatus.OK);
    }

    @Log("根据queryId获取impala任务的执行计划信息")
    @PostMapping("/getImpalaProfileById")
    public ResponseEntity<Object> getImpalaProfileById(String cmFlag, String clusterFlag, String queryId) {
        Object profile = statementAuditService.getImpalaProfileById(cmFlag, clusterFlag, queryId);
        return new ResponseEntity<>(profile, HttpStatus.OK);
    }

    @Log("按月创建tb_impala_parameter、tb_yarn_parameter月份表")
    @GetMapping("/createTableMonth")
    @AnonymousAccess
    public ResponseEntity<Object> createTableMonth() {
        statementAuditService.createTableMonth();
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除2年前tb_impala_parameter、tb_yarn_parameter月份表")
    @GetMapping("/dropTableMonth")
    @AnonymousAccess
    public ResponseEntity<Object> dropTableMonth() {
        statementAuditService.dropTableMonth();
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
